// pages/todo/detail.js
Page({
  data: {
    id: '',
    title: '',
    description: '',
    steps: [],
    planEndTime: '',
    actualEndTime: '',
    responsible: '',
    collaborators: [],
    supervisor: '',
    reminderTime: '',
    status: 'ongoing',
    isEdit: false
  },

  onLoad: function(options) {
    if (options.id) {
      this.setData({ isEdit: true });
      this.loadTodoDetail(options.id);
    }
  },

  // 加载待办详情
  loadTodoDetail: function(id) {
    const app = getApp();
    const todo = app.globalData.todos.find(t => t.id === id);
    if (todo) {
      this.setData({
        ...todo,
        planEndTime: todo.planEndTime.split('T')[0],
        reminderTime: todo.reminderTime ? todo.reminderTime.split('T')[0] : ''
      });
    }
  },

  // 输入标题
  onTitleInput: function(e) {
    this.setData({ title: e.detail.value });
  },

  // 输入描述
  onDescriptionInput: function(e) {
    this.setData({ description: e.detail.value });
  },

  // 添加工作步骤
  addStep: function() {
    const steps = this.data.steps;
    steps.push({ content: '', completed: false });
    this.setData({ steps });
  },

  // 编辑工作步骤
  onStepInput: function(e) {
    const index = e.currentTarget.dataset.index;
    const steps = this.data.steps;
    steps[index].content = e.detail.value;
    this.setData({ steps });
  },

  // 删除工作步骤
  deleteStep: function(e) {
    const index = e.currentTarget.dataset.index;
    const steps = this.data.steps;
    steps.splice(index, 1);
    this.setData({ steps });
  },

  // 设置计划完成时间
  onPlanEndTimeChange: function(e) {
    this.setData({ planEndTime: e.detail.value });
  },

  // 设置提醒时间
  onReminderTimeChange: function(e) {
    this.setData({ reminderTime: e.detail.value });
  },

  // 设置主责人
  onResponsibleInput: function(e) {
    this.setData({ responsible: e.detail.value });
  },

  // 设置协同人
  onCollaboratorsInput: function(e) {
    this.setData({ 
      collaborators: e.detail.value.split(',').map(item => item.trim())
    });
  },

  // 设置监督人
  onSupervisorInput: function(e) {
    this.setData({ supervisor: e.detail.value });
  },

  // 保存待办事项
  saveTodo: function() {
    if (!this.validateForm()) {
      return;
    }

    const app = getApp();
    const todoData = {
      id: this.data.id || Date.now().toString(),
      title: this.data.title,
      description: this.data.description,
      steps: this.data.steps,
      planEndTime: this.data.planEndTime + 'T00:00:00Z',
      actualEndTime: this.data.actualEndTime,
      responsible: this.data.responsible,
      collaborators: this.data.collaborators,
      supervisor: this.data.supervisor,
      reminderTime: this.data.reminderTime ? this.data.reminderTime + 'T00:00:00Z' : '',
      status: this.data.status
    };

    if (this.data.isEdit) {
      app.updateTodo(this.data.id, todoData);
    } else {
      app.addTodo(todoData);
    }

    // 如果设置了提醒时间，创建提醒
    if (todoData.reminderTime) {
      app.setReminder(todoData.id, todoData.reminderTime);
    }

    wx.navigateBack();
  },

  // 表单验证
  validateForm: function() {
    if (!this.data.title) {
      wx.showToast({
        title: '请输入标题',
        icon: 'none'
      });
      return false;
    }
    if (!this.data.planEndTime) {
      wx.showToast({
        title: '请选择计划完成时间',
        icon: 'none'
      });
      return false;
    }
    if (!this.data.responsible) {
      wx.showToast({
        title: '请输入主责人',
        icon: 'none'
      });
      return false;
    }
    if (!this.data.supervisor) {
      wx.showToast({
        title: '请输入监督人',
        icon: 'none'
      });
      return false;
    }
    return true;
  }
}));